package com.ruoyi.system.hanyun.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.hanyun.mapper.*;
import com.ruoyi.hanyun.domain.*;
import com.ruoyi.hanyun.service.IHyTasksService;

/**
 * 任务Service业务层处理
 */
@Service
public class HyTasksServiceImpl implements IHyTasksService {
    @Autowired
    private HyTasksMapper hyTasksMapper;

    @Autowired
    private HyTaskDependenciesMapper HyTaskDependenciesMapper;

    @Autowired
    private HyTaskAssignmentHistoryMapper hyTaskAssignmentHistoryMapper;

    /**
     * 查询任务
     *
     * @param id 任务ID
     * @return 任务
     */
    @Override
    public HyTasks selectHyTaskById(Long id) {
        return hyTasksMapper.selectHyTaskById(id);
    }

    /**
     * 查询任务列表
     *
     * @param hyTask 任务
     * @return 任务
     */
    @Override
    public List<HyTasks> selectHyTaskList(HyTasks hyTask) {
        return hyTasksMapper.selectHyTaskList(hyTask);
    }

    /**
     * 新增任务
     *
     * @param hyTask 任务
     * @return 结果
     */
    @Override
    public int insertHyTask(HyTasks hyTask) {
        return hyTasksMapper.insertHyTask(hyTask);
    }

    /**
     * 修改任务
     *
     * @param hyTask 任务
     * @return 结果
     */
    @Override
    public int updateHyTask(HyTasks hyTask) {
        return hyTasksMapper.updateHyTask(hyTask);
    }

    /**
     * 批量删除任务
     *
     * @param ids 需要删除的任务ID
     * @return 结果
     */
    @Override
    public int deleteHyTaskByIds(Long[] ids) {
        return hyTasksMapper.deleteHyTaskByIds(ids);
    }

    /**
     * 删除任务信息
     *
     * @param id 任务ID
     * @return 结果
     */
    @Override
    public int deleteHyTaskById(Long id) {
        return hyTasksMapper.deleteHyTaskById(id);
    }

    /**
     * 查询子任务列表
     *
     * @param parentId 父任务ID
     * @return 子任务列表
     */
    @Override
    public List<HyTasks> selectHyTaskChildren(Long parentId) {
        return hyTasksMapper.selectHyTaskChildren(parentId);
    }

    /**
     * 更新任务状态
     *
     * @param taskId 任务ID
     * @param status 新状态
     * @return 结果
     */
    @Override
    public int updateTaskStatus(Long taskId, String status) {
        HyTasks task = new HyTasks();
        task.setId(taskId);
        task.setStatus(status);
        return hyTasksMapper.updateHyTask(task);
    }

    /**
     * 更新任务进度
     *
     * @param taskId 任务ID
     * @param progress 进度百分比
     * @return 结果
     */
    @Override
    public int updateTaskProgress(Long taskId, Integer progress) {
        HyTasks task = new HyTasks();
        task.setId(taskId);
        task.setProgress(progress);
        return hyTasksMapper.updateHyTask(task);
    }

    /**
     * 分配任务
     *
     * @param taskId 任务ID
     * @param ownerId 负责人ID
     * @param assignedBy 分配人ID
     * @return 结果
     */
    @Override
    @Transactional
    public int assignTask(Long taskId, Long ownerId, Long assignedBy) {
        // 更新任务负责人
        HyTasks task = new HyTasks();
        task.setId(taskId);
        task.setOwnerId(ownerId);
        int rows = hyTasksMapper.updateHyTask(task);

        if (rows > 0) {
            // 记录分配历史
            HyTaskAssignmentHistory history = new HyTaskAssignmentHistory();
            history.setTaskId(taskId);
            history.setOwnerId(ownerId);
            history.setAssignedBy(assignedBy);
            hyTaskAssignmentHistoryMapper.insertHyTaskAssignmentHistory(history);
        }

        return rows;
    }

    /**
     * 添加任务依赖
     *
     * @param taskId 任务ID
     * @param dependsOnTaskId 依赖的任务ID
     * @return 结果
     */
    @Override
    public int addTaskDependency(Long taskId, Long dependsOnTaskId) {
        HyTaskDependencies dependency = new HyTaskDependencies();
        dependency.setTaskId(taskId);
        dependency.setDependsOnTaskId(dependsOnTaskId);
        return HyTaskDependenciesMapper.insertHyTaskDependencies(dependency);
    }

    /**
     * 移除任务依赖
     *
     * @param taskId 任务ID
     * @param dependsOnTaskId 依赖的任务ID
     * @return 结果
     */
    @Override
    public int removeTaskDependency(Long taskId, Long dependsOnTaskId) {
        HyTaskDependencies dependency = new HyTaskDependencies();
        dependency.setTaskId(taskId);
        dependency.setDependsOnTaskId(dependsOnTaskId);
        return HyTaskDependenciesMapper.deleteHyTaskDependenciesByTaskId(taskId);
    }
}